﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ClassRoom.Controls
{
    public partial class ColorPicker : UserControl
    {
        public Color SelectedColor;
        private bool _IsHueMoniterCaptureMouse = false;

        public ColorPicker()
        {
            InitializeComponent();
            this.SelectedColor = GetColor();
        }

        private void HueMonitor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.HueMonitor.CaptureMouse();
            this._IsHueMoniterCaptureMouse = true;
            DragSliders(e.GetPosition((UIElement)sender).Y);
        }

        private void HueMonitor_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.HueMonitor.ReleaseMouseCapture();
            this._IsHueMoniterCaptureMouse = false;
            this.SelectedColor = GetColor();
        }

        private void HueMonitor_MouseMove(object sender, MouseEventArgs e)
        {
            if (this._IsHueMoniterCaptureMouse)
            {
                DragSliders(e.GetPosition((UIElement)sender).Y);
            }
        }

        private void DragSliders(double y)
        {
            if (y < 0)
                this.HueSelector.SetValue(Canvas.TopProperty, 0.0 - this.HueSelector.Height / 2);
            else if (y > this.HueMonitor.Height)
                this.HueSelector.SetValue(Canvas.TopProperty, this.HueMonitor.Height - this.HueSelector.Height / 2);
            else
                this.HueSelector.SetValue(Canvas.TopProperty, y - this.HueSelector.Height / 2);
        }

        private Color GetColor()
        {
            double hueComponent = (((double)this.HueSelector.GetValue(Canvas.TopProperty) + this.HueSelector.Height / 2) / this.HueMonitor.Height) * 360;

            return ColorSpace.GetColorFromPosition(hueComponent);
        }
    }
}
